package Stack;

/**
 * User: cool
 * Date: 2017/11/14
 * Time: 17:08
 */
public class LinkedStack {
    Node top = null;
    int length = 0;

    public boolean push(Object object) {

        Node node = new Node(object);
        node.setNextNode(top);
        top = node;
        length++;
        return true;
    }
    public Node pop () {
        Node popNode = top;
        top = top.getNextNode();

        length -- ;

        popNode.setNextNode(null);
        return popNode;
    }
    public Node getTop(){

        return top;
    }

    public String toString(){

        StringBuffer stringBuffer = new StringBuffer();
        if(top!=null){
            stringBuffer.append(top.toString());
            Node currenNode = top.getNextNode();
            while (currenNode !=null){
                stringBuffer.append(currenNode.toString());
                currenNode = currenNode.getNextNode();
            }
            return stringBuffer.toString();

        }

        return "";
    }

    public void clear(){
        this.top = null;
        this.length = 0;
    }
}
